package easy;

public class Solution_2047 {

    public int countValidWords(String sentence) {
        sentence += " ";
        char[] chars = sentence.toCharArray();
        int count = 0;
        for (int i = 0, num = 0, count_num = 0, count_split = 0, index_split = -1, count_comma = 0, index_comma = -1;
            i < chars.length;
            i++) {
            char c = chars[i];
            if (c != ' ') {
                if (c == '-') {
                    count_split++;
                    index_split = i;
                } else if (c == '!' || c == '.' || c == ',') {
                    count_comma++;
                    index_comma = i;
                } else if (c >= '0' && c <= '9') {
                    count_num++;
                }
                num++;
            } else {
                if (num > 0 && count_num == 0 && count_split <= 1 && count_comma <= 1) {
                    if ((count_comma == 0 || index_comma == i - 1)
                        && (count_split == 0 || (index_split != (i - 1 - count_comma) && index_split != (i - num)))) {
                        count++;
                    }
                }
                num = 0;
                count_num = 0;
                count_split = 0;
                index_split = -1;
                count_comma = 0;
                index_comma = -1;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Solution_2047 model = new Solution_2047();
        System.out.println(model.countValidWords("cat and  dog"));
        System.out.println(model.countValidWords("!this  1-s b8d!"));
        System.out.println(model.countValidWords("alice and  bob are playing stone-game10"));
        System.out.println(model.countValidWords("he bought 2 pencils, 3 erasers, and 1  pencil-sharpener."));
    }
}
